package com.im.project.controller;

import com.im.common.result.JsonResult;
import com.im.project.entity.dto.AddOrRemoveClusterUserDto;
import com.im.project.entity.dto.ClusterIdDto;
import com.im.project.entity.dto.CreateClusterDto;
import com.im.project.entity.dto.UpdateClusterDto;
import com.im.project.service.ClusterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 群组 控制层
 *
 * @author yunZhouJiaDi
 */
@RestController
@RequestMapping("/cluster")
@Api(tags = "ClusterController", description = "群组相关接口")
public class ClusterController {

    @Resource
    private ClusterService clusterService;

    @PostMapping("/create")
    @ApiOperation("创建群组")
    public JsonResult createCluster(@Validated @RequestBody CreateClusterDto createClusterDto) {
        return JsonResult.success(clusterService.createCluster(createClusterDto));
    }

    @GetMapping("/searchGroup")
    @ApiOperation("搜索群组")
    public JsonResult queryContact(@RequestParam String searchKey) {
        return JsonResult.success(clusterService.queryGroup(searchKey));
    }

    @GetMapping("/my/list")
    @ApiOperation("当前登录用户群组列表")
    public JsonResult queryCurrentClusterList() {
        return JsonResult.success(clusterService.queryCurrentClusterList());
    }

    @GetMapping("/join/list")
    @ApiOperation("当前用户加入的群组列表")
    public JsonResult queryGroupListByUserId() {
        return JsonResult.success(clusterService.queryGroupListByUserId());
    }

    @GetMapping("/detail")
    @ApiOperation("查询指定群信息")
    public JsonResult queryClusterByClusterId(@RequestParam String clusterId) {
        return JsonResult.success(clusterService.queryClusterByClusterId(clusterId));
    }

    @PostMapping("/update")
    @ApiOperation("修改群组")
    public JsonResult updateCluster(@Validated @RequestBody UpdateClusterDto updateClusterDto) {
        return JsonResult.success(clusterService.updateCluster(updateClusterDto));
    }

    @PostMapping("/leave")
    @ApiOperation("退出群组")
    public JsonResult leaveCluster(@Validated @RequestBody ClusterIdDto clusterIdDto) {
        return JsonResult.success(clusterService.leaveCluster(clusterIdDto));
    }

    @PostMapping("/dissolution")
    @ApiOperation("解散群组")
    public JsonResult dissolutionCluster(@Validated @RequestBody ClusterIdDto clusterIdDto) {
        return JsonResult.success(clusterService.dissolutionCluster(clusterIdDto));
    }

    @GetMapping("/chat")
    @ApiOperation("查询指定群会话")
    public JsonResult queryClusterUserChatByClusterId(@RequestParam String clusterId) {
        return JsonResult.success(clusterService.queryClusterUserChatByClusterId(clusterId));
    }

    @PostMapping("/addClusterUser")
    @ApiOperation("添加群组成员")
    public JsonResult addClusterUser(@Validated @RequestBody AddOrRemoveClusterUserDto addOrRemoveClusterUserDto) {
        return JsonResult.success(clusterService.addClusterUsers(addOrRemoveClusterUserDto));
    }

    @PostMapping("/delClusterUser")
    @ApiOperation("删除群组成员")
    public JsonResult delClusterUser(@Validated @RequestBody AddOrRemoveClusterUserDto addOrRemoveClusterUserDto) {
        return JsonResult.success(clusterService.delClusterUser(addOrRemoveClusterUserDto));
    }

}
